// global variables //
aeroui.TIMER = 2;
aeroui.SPEED = 10;
aeroui.DISABLED = [];
//var WRAPPER = 'content';


// build/show the dialog box, populate the data and call the fadeDialog function //
/**
 * @param title 是指window的title
 * @param _url 是指要用于ajax请求的_url
 * @param type 是window的类型
 * @param autohide 是否自动消失
 * @param successCallBack 成功之后的回调
 * @param width dialog的width 默认为window的width/3
 * @param heigth dialog的height 默认为window的height/3
 */
function showDialog(title,_url,_width,_height,successCallBack,type,autohide) {
  // 查找全部没有select下拉框，并它们都disabled
//  aeroui.DISABLED = $('select[disabled!="disabled"]');
//  $('select[disabled!="disabled"]').each(function(){
//  	$(this).attr('disabled' , 'disabled');
//  });
  
  // 处理select的hide
	hideSelect();
  
  if(!type) {
    type = 'error';
  }
    var dialog = document.getElementById('dialog');
    var dialogheader = document.getElementById('dialog-header');
    var dialogtitle = document.getElementById('dialog-title');
    var dialogclose = document.getElementById('dialog-close');
    var dialogcontent = document.getElementById('dialog-content');
    var dialogmask = document.getElementById('dialog-mask');
    dialogmask.style.visibility = "visible";
    dialogmask.style.height = document.body.scrollHeight + 'px';
    
//    dialogmask.style.top = document.body.scrollTop + 'px';
//    dialogmask.style.left = document.body.scrollLeft + 'px';
    
    
    dialog.style.visibility = "visible";
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;

  // 清空div的内容
  dialogtitle.innerHTML = '';
  dialogcontent.innerHTML = '';
  /**
	 * 把要用的div设置为可见
	 *************************************/
  if(dialog.style.display == 'none'){
  	dialog.style.display = '';
  }
  if(dialogmask.style.display == 'none'){
  	dialogmask.style.display = '';
  }
  
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;
  
//  var _left = window.screen.availWidth / 2 - _width / 2;
//  var _top = window.screen.availHeight / 3 - _height / 2;
//	alert(document.body.clientHeight + " >> " + document.body.clientWidth + " >> \n" +
//			"" + window.screen.height + " >> " + window.screen.width + " >> \n" +
//			"" + window.screen.availHeight + " >> " + window.screen.availWidth + " >> \n" +
//			"" + document.body.scrollHeight + " >> ");
  var _left = window.screen.width / 2 - _width / 2;
  var _top = window.screen.height / 3 - _height / 2;
  
 //  _top = _top - ( document.body.scrollHeight - window.screen.height ) / 2 ;
  
//  alert( document.body.scrollHeight );
  
 _top = _top + document.body.scrollTop;
 
  
//  alert( _top + " >> " + document.documentElement.scrollTop );
  
  dialog.style.top =  _top  + "px";
  dialog.style.left = _left + "px";
  dialogheader.className = type + "header";
  dialogheader.style.width = _width  + 'px';
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.style.width = _width  + 'px';
  dialogcontent.style.height = _height  + 'px';
  // 为window的加载提供进度信息
  dialogcontent.style.backgroundImage = 'url("common/js/dialog/images/blue-loading.gif")';
  dialogcontent.style.backgroundPosition = 'center';
  dialogcontent.style.backgroundRepeat = 'no-repeat';
  /*
   * ajax请求得到html
   */
   // alert(_url);
   $.ajax({
   		url : encodeURI(_url),
   		type : 'POST',
   		cache : false,
   		success : function(result){
//			 alert(result);
			if(result != null ){
	   			var _content = document.getElementById('dialog-content');
				/*
				 * 使用了jquery的事件
				 */
				$('#dialog-content').html(result);
				// 去除进度信息
				dialogcontent.style.backgroundImage = '';
				dialogcontent.style.backgroundPosition = 'bottom right';
			}
			// 执行回调
			if(typeof(successCallBack) != 'undefined' && successCallBack.constructor == Function){
				successCallBack(result);
			}
   		}
   });
  /***********************************************
   * 添加
   ***********************************************/
   // 如果是IE时，会出现dialog的title与content的width是不一样的，所以此时取title的width为content的width
   if(document.all){
   	dialogcontent.style.width = dialogheader.offsetWidth + 'px';
   }
   /***********************************************
   * 添加
   ***********************************************/
   
  dialog.timer = setInterval("fadeDialog(1)", aeroui.TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }
  $("#dialog-mask").bgiframe();
}

// hide the dialog box //
// 关闭窗口之前的回调方法
function hideDialog() {
	/**
	 * 把层也隐藏
	 */
  var dialog = document.getElementById('dialog');
	if(dialog.style.display == ''){
		dialog.style.display = 'none';
  	}
  var dialogmask = document.getElementById('dialog-mask');
  if(dialogmask.style.display == ''){
  	dialogmask.style.display = 'none';
  }
  window.clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", aeroui.TIMER);
  
  // show select
	showSelect();
  
//  aeroui.DISABLED.each(function(){
//  	$(this).attr('disabled' , '');
//  });
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + aeroui.SPEED;
  } else {
    value = dialog.alpha - aeroui.SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}